package com.coder.shop.mq.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import com.coder.shop.dao.model.BaseMsgCode;

/**
 * 验证码消息接收器
 * 
 * @author liuzm
 *
 */
public class MsgCodeReceiver implements MessageListener {

	private static String operation = "/industrySMS/sendSMS";

	@Override
	public void onMessage(Message message) {
		if (message instanceof ObjectMessage) {
			ObjectMessage omsg = (ObjectMessage) message;
			try {
				BaseMsgCode msgCode = (BaseMsgCode) omsg.getObject();
				execute(msgCode);
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 验证码通知短信
	 */
	public void execute(BaseMsgCode msgCode) {
		String url = Config.BASE_URL + operation;
		String body = "accountSid=" + Config.ACCOUNT_SID + "&to=" + msgCode.getPhone() + "&templateid=" + 236216821 + "&param="+msgCode.getCode()+",10"
				+ HttpUtil.createCommonParam();

		// 提交请求
		String result = HttpUtil.post(url, body);
		System.out.println("result:" + System.lineSeparator() + result);
	}

}
